package com.example.controller;

import com.example.common.Code;
import com.example.common.Result;
import com.example.domain.Department;
import com.example.service.DepartmentService;
import com.example.utils.ExcelUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author luo
 * 部门管理
 */
@RestController
@RequestMapping("/departments")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    public Result selectAllDepart() {
        List<Department> departments = departmentService.selectAllDepart();
        String msg = departments != null ? "" : "数据设置失败，请重试！";
        Integer code = departments != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, departments, msg);
    }

    @GetMapping("/{id}")
    public Result selectDepartById(@PathVariable Integer id) {
        Department departments = null;

        String key = "Department_ID：" +id;
        //先从redis中获取缓存数据
        departments = (Department) redisTemplate.opsForValue().get(key);

        if(departments != null){
            //如果存在，直接返回，无需查询数据库
            return new Result(Code.GET_OK,departments,"查询成功！");
        }
        departments = departmentService.selectDepartmentById(id);
        redisTemplate.opsForValue().set(key,departments,60, TimeUnit.MINUTES);

        String msg = departments!=null ? "" : "数据查询失败，请重试！";
        Integer code = departments != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code,departments,msg);
    }

    @PostMapping("/ByName")
    public Result selectDepartmentByName(@RequestBody Department department) {
        List<Department> departments = departmentService.selectDepartmentByName(department);
        String msg = departments != null ? "" : "数据查询失败，请重试！";
        Integer code = departments != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, departments, msg);
    }

    @PostMapping
    public Result insertDepartment(@RequestBody Department department) {
        int i = departmentService.addDepartment(department);
        String msg = i > 0 ? "" : "数据查询失败，请重试！";
        if (i > 0) {
            System.out.println("插入成功");
            return new Result(Code.INSERT_OK, i, msg);
        } else {
            System.out.println("插入失败");
            return new Result(Code.INSERT_ERR, i, msg);
        }
    }

    @PutMapping
    public Result updateDepartment(@RequestBody Department department) {
        int i = departmentService.updateDepartment(department);
        String msg = i > 0 ? "" : "数据更新失败，请重试！";

        //删除redis中缓存
        String key = "Department_ID：" +department.getId();
        redisTemplate.delete(key);

        if (i > 0) {
            System.out.println("更新成功");
            return new Result(Code.UPDATE_OK, i, msg);
        } else {
            System.out.println("更新失败");
            return new Result(Code.UPDATE_ERR, i, msg);
        }
    }

    @DeleteMapping("/{id}")
    public Result deleteDepartment(@PathVariable Integer id) {
        int i = departmentService.deleteDepartment(id);
        String msg = i > 0 ? "" : "数据删除失败，请重试！";

        //删除redis中缓存
        String key = "Department_ID：" +id;
        redisTemplate.delete(key);

        if (i > 0) {
            System.out.println("删除成功");
            return new Result(Code.DELETE_OK, i, msg);
        } else {
            System.out.println("删除失败");
            return new Result(Code.DELETE_OK, i, msg);
        }
    }

    @GetMapping("/{page}/{size}")
    public Result findUserList(@PathVariable int page, @PathVariable int size) {
        PageInfo<Department> page1 = departmentService.findPage(page, size);
        Integer code = page1 != null ? Code.GET_OK : Code.GET_ERR;
        String msg = page1 != null ? "" : "分页失败，请重试！";
        return new Result(code, page1, msg);
    }

    /**
     * 导出excel表
     */

    @RequestMapping("/makeExcel")
    public void makeExcel(HttpServletResponse response) throws Exception {
        //打印Excel表
        ExcelUtils.exportExcel(response, "department.xlsx", departmentService.getOutputExcelProjectData());
    }
}

